package com.xms.queryview.api.controller;

import com.xms.core.exception.XmsException;
import com.xms.queryview.api.model.SetViewDefaultStateModel;
import com.xms.queryview.api.model.UpdateQueryViewModel;
import com.xms.queryview.entity.QueryView;
import com.xms.queryview.service.IQueryViewFinderService;
import com.xms.queryview.service.IQueryViewUpdaterService;
import com.xms.sdk.core.query.QueryExpression;
import com.xms.utils.CollectionUtil;
import com.xms.utils.JsonUtil;
import com.xms.utils.StringUtil;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import com.xms.web.framework.model.SetRecordStateModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * QueryViewUpdaterController
 * </p>
 *
 * @author migo
 * @since 2021/9/18
 */
@RestController
@RequestMapping("{org}/api/schema/queryview")
public class QueryViewUpdaterController extends ApiBaseController {
    private IQueryViewUpdaterService _queryViewUpdaterService;
    private IQueryViewFinderService _queryViewFinderService;

    @Autowired
    public void QueryViewUpdaterController(IQueryViewUpdaterService queryViewUpdaterService,
                                           IQueryViewFinderService queryViewFinderService) {
        _queryViewUpdaterService = queryViewUpdaterService;
        _queryViewFinderService = queryViewFinderService;
    }

    @ApiOperation("重新格式化查询表达式")
    @GetMapping("formatfetchconfig")
    public JsonResultObject formatFetchconfig() {
        List<QueryView> all = _queryViewFinderService.all();
        for (QueryView item : all) {
            try {
                QueryExpression exp = JsonUtil.toBean(item.getFetchconfig(), QueryExpression.class);
                item.setFetchconfig(JsonUtil.toJSONString(exp));
                _queryViewUpdaterService.update(item);
            } catch (Exception e) {
                e.printStackTrace();
                return JError(item.getName() + " 出错：" + e.getMessage());
            }
        }
        return JOk();
    }

    @ApiOperation("修改视图")
    @PostMapping("update")
    public JsonResultObject update(@RequestBody UpdateQueryViewModel model) throws XmsException, SQLException {
        if (UUIDUtil.isEmpty(model.queryviewid)) {
            return JNotSpecifiedParams("queryviewid");
        }
        if (StringUtil.isEmpty(model.name)) {
            return JNotSpecifiedParams("name");
        }
        QueryView view = _queryViewFinderService.getById(model.queryviewid);
        if (view == null) {
            return JNotFound();
        }
        view.setName(model.name);
        view.setIscustombutton(model.iscustombutton);
        if (model.iscustombutton && CollectionUtil.notEmpty(model.buttonid)) {
            view.setCustombuttons(JsonUtil.toJSONString(model.buttonid));
        }
        else{
            view.setCustombuttons("");
        }
        view.setDescription(model.description);
        view.setFetchconfig(model.fetchconfig);
        view.setLayoutconfig(model.layoutconfig);
        view.setAggregateconfig(model.aggregateconfig);
        view.setTargetformid(model.targetformid);
        return _queryViewUpdaterService.update(view) ? JUpdateSuccess() : JUpdateFailure();
    }

    @ApiOperation("设置视图可用状态")
    @PostMapping("setstate")
    public JsonResultObject setState(@RequestBody SetRecordStateModel model) {
        if (CollectionUtil.isEmpty(model.recordid)) {
            return JNotSpecifiedParams("id");
        }
        if (_queryViewUpdaterService.updateState(model.isenabled, model.recordid)) {
            return JUpdateSuccess();
        }
        return JUpdateFailure();
    }

    @ApiOperation("设置默认视图")
    @PostMapping("setdefault")
    public JsonResultObject setDefault(@RequestBody SetViewDefaultStateModel model) {
        if (CollectionUtil.isEmpty(model.recordId) || CollectionUtil.isEmpty(model.entityId)) {
            return JNotSpecifiedParams("id", "entityid");
        }
        if (_queryViewUpdaterService.updateDefault(model.entityId, model.recordId[0])) {
            return JUpdateSuccess();
        }
        return JUpdateFailure();
    }
}
